package com.fmh.mybatisplus.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fmh.mybatisplus.comoon.vo.Const;
import com.fmh.mybatisplus.comoon.vo.Result;
import com.fmh.mybatisplus.mapper.RoleMapper;
import com.fmh.mybatisplus.pojo.Role;
import com.fmh.mybatisplus.pojo.RoleMenu;
import com.fmh.mybatisplus.service.RoleMenuService;
import com.fmh.mybatisplus.service.RoleService;
import com.fmh.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController{

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id) {
        // 单个角色信息
        Role role = roleService.getById(id);

        // 已有权限信息
        List<RoleMenu> roleMenus = roleMenuService.list(new QueryWrapper<RoleMenu>().eq("role_id", id));

        // 流转换
        List<Long> menuIds = roleMenus.stream().map(p -> p.getMenuId()).collect(Collectors.toList());

        role.setMenuIds(menuIds);
        return Result.success(menuIds);
    }

    @GetMapping("/list")
    public Result list(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam String name
    ) {
        Page<Role> page = new Page<>(current, size);
        Page<Role> rolePage = roleService.page(page,
                new QueryWrapper<Role>().like( "name", name) // name不为空时，再查询
        );

        // 查询所拥有的角色
//        menuIds
//        rolePage.getRecords().forEach(u -> {
//            u.setMenuIds(roleService.listRolesByUserId(u.getId()));
//        });

        return Result.success(rolePage);
    }

    @PostMapping("/add")
    public Result add(@Validated @RequestBody Role role) {
        //状态
        role.setStatus(Const.STATUS_ON);
        //保存
        roleService.save(role);
        //返回
        return Result.success(role);
    }

    @PostMapping("/update")
    public Result update(@Validated @RequestBody Role role) {
        roleService.updateById(role);
        return Result.success(role);
    }

    @PostMapping("/del")
    public Result delete(@RequestBody Long[] ids) {
        roleService.removeByIds(Arrays.asList(ids));

        return Result.success("");

    }

    @PostMapping("/prs/{roleId}")
    public Result permission(@PathVariable(name = "roleId") Long roleId, @RequestBody long[] menuIds) {
        // 存储拆分的数据
        ArrayList<RoleMenu> roleMenus  = new ArrayList<>();

        // 拆分成多条记录
        Arrays.stream(menuIds).forEach(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            //保存
            roleMenus.add(roleMenu);
        });

        // 先删除原来的记录，再保存新的
        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
        roleMenuService.saveBatch(roleMenus);

        //删除redis缓存

        return Result.success(menuIds);
    }

}
